import 'package:dio/dio.dart';

/// 日志打印拦截器
class AppLogInterceptor extends Interceptor {
  final bool enableLog;

  AppLogInterceptor({this.enableLog = true});

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    if (enableLog) {
      print('--> ${options.method} ${options.path}');
      print('Headers: ${options.headers}');
      print('Data: ${options.data}');
    }
    super.onRequest(options, handler);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    if (enableLog) {
      print('<-- ${response.statusCode} ${response.requestOptions.path}');
      print('Response: ${response.data}');
    }
    super.onResponse(response, handler);
  }

  @override
  void onError(DioException err, ErrorInterceptorHandler handler) {
    if (enableLog) {
      print('❌ ERROR [${err.response?.statusCode}] => ${err.message}');
    }
    super.onError(err, handler);
  }
}

/// token 拦截器（可以自动加 token，也可刷新）
class TokenInterceptor extends Interceptor {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    // 你可以统一添加 token（或从 storage 中读取）
    // options.headers['Authorization'] = 'Bearer ${TokenManager.token}';
    super.onRequest(options, handler);
  }
}
